package com.zhuoyue.web.controller;


import com.zhuoyue.biz.core.api.dto.rep.FingerprintRepDTO;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.zhuoyue.biz.core.api.domain.FingerprintDO;
import com.zhuoyue.biz.core.api.service.FingerprintService;
import com.zhuoyue.common.dto.ControllerResult;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * 指纹锁操作
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/fingerprint")
@Validated
@Slf4j
/**
 * @ClassName: AppFingerprintController
 * @Description:指纹锁指纹信息编辑获取，修改,删除接口
 * @author wjming
 * @Date 2021/2/14 19:53
 */
public class AppFingerprintController {

    @Autowired
    private FingerprintService fingerprintService;

    /**
     * 查询指纹锁录入信息
     *
     * @param deviceId
     * @return
     */
    @GetMapping("/list")
    @CheckLogin
    public ControllerResult getFingerprintListByDeviceId(@NotBlank(message = "指纹锁设备ID不能为空！") String deviceId, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [getFingerprintListByDeviceId] -> [deviceId:{}]", user.getAccount(), user.getOrgCode(), deviceId);
        List<FingerprintDO> result = fingerprintService.getFingerprintListByDeviceId(deviceId);
        List<FingerprintRepDTO> fingerprintRepDtoS = new ArrayList<>(5);
        result.stream().forEach(relist -> {
            fingerprintRepDtoS.add(repDTO(relist));
        });
        log.info("result -> user:[{}_{}] [getFingerprintListByDeviceId] -> [{}]", user.getAccount(), user.getOrgCode(), fingerprintRepDtoS);
        return ControllerResult.toReturnDataSuccess("查询成功", fingerprintRepDtoS);
    }


    /**
     * 指纹信息录入
     *
     * @param deviceId
     * @param number
     * @param name
     * @param sceneId
     * @return
     */
    @PostMapping("/add")
    @CheckLogin
    public ControllerResult addFingerprint(@NotBlank(message = "设备ID不能为空！") String deviceId, @NotBlank(message = "指纹编号不能为空！") String number,
                                           @Length(min = 1, max = 15, message = "请输入1-15个字符") String name, String sceneId, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [addFingerprint] -> [deviceId:{},number:{},name:{}]", user.getAccount(), user.getOrgCode(), deviceId, number, name);
        if (StringUtils.isEmpty(name)) {
            name = number + "号指纹";
        }
        FingerprintDO fingerprint = new FingerprintDO();
        fingerprint.setDeviceId(deviceId);
        fingerprint.setNumber(number);
        fingerprint.setName(name);
        fingerprint.setSceneId(StringUtils.isNotEmpty(sceneId) ? sceneId : null);
        FingerprintDO result = fingerprintService.addFingerprint(fingerprint);
        FingerprintRepDTO fingerprintRepDTO = repDTO(result);
        log.info("result -> user:[{}_{}] [addFingerprint] -> [fingerprintRepDTO:{}]", user.getAccount(), user.getOrgCode(), fingerprintRepDTO);
        return ControllerResult.toReturnDataSuccess("指纹锁录入成功", fingerprintRepDTO);
    }

    /**
     * 修改指纹信息
     *
     * @param id
     * @param name
     * @param sceneId
     * @return
     */
    @PutMapping("/modify")
    @CheckLogin
    public ControllerResult updateFingerprint(@NotBlank(message = "指纹信息ID不能为空！") String id,
                                              @Length(max = 15, message = "不能超过15个字符") String name,
                                              String sceneId, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [updateFingerprint] -> [id:{},number:{},name:{}]", user.getAccount(), user.getOrgCode(), id, name, sceneId);
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(sceneId)) {
            return ControllerResult.toParamFail("请确认修改信息是否正确!");
        }
        FingerprintDO fingerprint = new FingerprintDO();
        fingerprint.setId(id);
        fingerprint.setName(name);
        fingerprint.setSceneId(sceneId);
        FingerprintDO result = fingerprintService.updateFingerprintById(fingerprint);
        FingerprintRepDTO fingerprintRepDTO = repDTO(result);
        log.info("result -> user:[{}_{}] [updateFingerprint] -> [{},]", user.getAccount(), user.getOrgCode(),fingerprintRepDTO);
        return ControllerResult.toReturnDataSuccess("修改成功", fingerprintRepDTO);
    }


    /**
     * @return
     */
    @DeleteMapping("/delete")
    @CheckLogin
    public ControllerResult deleteFingerprint(@NotNull("ID不能为空") String id, HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        log.info("request -> user:[{}_{}] [updateFingerprint] -> [id:{}]", user.getAccount(), user.getOrgCode(), id);
        fingerprintService.removeById(id);
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }

    private FingerprintRepDTO repDTO(FingerprintDO fingerprintDO) {
        FingerprintRepDTO fingerprintRepDTO = new FingerprintRepDTO();
        BeanUtils.copyProperties(fingerprintDO, fingerprintRepDTO);
        return fingerprintRepDTO;
    }
}
